﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Ribbon;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DonetArxLib
{
    internal static class CommonUtil
    {

        public static void WblockObject(Database db, ObjectIdCollection ids)
        {
            var dbNew = db.Wblock(ids, Point3d.Origin);
            dbNew.SaveAs("C:\\test.dwg", DwgVersion.AC1500);
        }


        public static void PurgeBlocksRecursively(Database db)
        {
            using (var trans = db.TransactionManager.StartTransaction()) 
            {
                var blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                if (blkTbl != null)
                {
                    while (PurgeBlocks(db, blkTbl, trans) > 0)
                    { }
                }

                trans.Commit();
            }
        }

        public static int PurgeBlocks(Database db, BlockTable blkTbl, Transaction trans)
        {
            int count = 0;
            var ids = new ObjectIdCollection();
            foreach (var id in blkTbl)
                ids.Add(id);

            db.Purge(ids);
            foreach (ObjectId id in ids)
            {
                var rcd = trans.GetObject(id, OpenMode.ForWrite) as BlockTableRecord;
                if (rcd == null)
                    continue;

                try
                {
                    rcd.Erase(true);
                    count++;
                }
                catch (System.Exception ex)
                {
                    UtilFunc.PrintMessage(ex.Message);
                }
            }

            return count;
        }

        /// <summary>
        /// 加载cuix文件
        /// </summary>
        /// <param name="cuixFile"></param>
        public static void LoadMenuFile(string cuixFile)
        {
            Application.LoadPartialMenu(cuixFile);
        }

        public static void AuditAndPurge()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            ed.Command("audit", "y");
            ed.Command("-purge", "a", "\n", "n");

            // save...
        }


        public static void PurgeFunction()
        {
            var db = Application.DocumentManager.MdiActiveDocument.Database;
            CommonUtil.PurgeBlocksRecursively(db);

            Application.ShowAlertDialog($"done!");
        }


        public static void DetectHyperLink()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            UtilFunc.IterateBlocksInBlockTable((id, trans) =>
            {

                var entity = trans.GetObject(id, OpenMode.ForRead) as Entity;
                if (entity != null)
                {
                    if (entity.Hyperlinks.Count > 0)
                    {
                        ed.WriteMessage($"entity with link:{entity.Handle}");
                    }
                }

                return true;
            });
        }
    }
}
